<template>
  <div class="operationGuideList pd-20">
    <el-input v-model="param.title"
              placeholder="请输入标题"
              clearable
              class="w-200"
              @clear="handleQuery"></el-input>
    <el-select v-model="param.type"
               placeholder="请选择客户端"
               clearable
               @change="handleQuery">
      <el-option v-for="item in options"
                 :key="item.value"
                 :label="item.label"
                 :value="item.value">
      </el-option>
    </el-select>
    <el-button type="primary"
               @click="handleQuery">查 询</el-button>
    <el-button @click="handleAdd"
               type="primary">新 增</el-button>
    <el-table stripe
              border
              :data="tableData"
              style="width: 100%"
              class="mt-20">
      <el-table-column align="center"
                       type="index"
                       min-width="20">
      </el-table-column>
      <el-table-column align="center"
                       prop="title"
                       label="标题"
                       width="800">
      </el-table-column>
      <el-table-column align="center"
                       prop="sortNum"
                       label="排序号"
                       width="100">
      </el-table-column>
      <el-table-column align="center"
                       label="客户端"
                       :formatter="formatterType"
                       width="100">
      </el-table-column>
      <el-table-column align="center"
                       prop="createTime"
                       label="发布时间"
                       min-width="120"
                       :formatter="formatterDate">
      </el-table-column>
      <el-table-column align="center"
                       width="180"
                       label="操作">
        <template slot-scope="scope">
          <el-button type="primary"
                     @click="handleEdit(scope.row)">编 辑</el-button>
          <el-button type="danger"
                     @click="handleDel(scope.row)">删 除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <page @getList="getList"
          :pageTotal="pageTotal"
          ref="page"></page>
    <el-dialog v-if="dialogVisible"
               fullscreen
               center
               append-to-body
               :show-close="false"
               :visible.sync="dialogVisible">
      <span slot="title">
        <span class="formTitle">新增/编辑</span>
        <el-button class="return"
                   type="primary"
                   @click="closeForm">返 回</el-button>
      </span>
      <el-form class="editorForm"
               ref="form"
               :rules="rules"
               :model="param"
               label-width="80px">
        <div ref="editor"
             class="editor">
          <el-form-item prop="content">
            <vue-neditor-wrap :destroy="true"
                              v-model="param.content"
                              :config="myConfig"></vue-neditor-wrap>
          </el-form-item>
        </div>
        <div class="info">
          <el-form-item label="标题"
                        prop="title">
            <el-input v-model="param.title"></el-input>
          </el-form-item>
          <el-form-item label="客户端"
                        prop="type">
            <el-select v-model="param.type"
                       placeholder="请选择客户端..."
                       clearable>
              <el-option v-for="item in options"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="排序号"
                        prop="sortNum">
            <el-input v-model.number="param.sortNum"></el-input>
          </el-form-item>
        </div>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="closeForm">取 消</el-button>
        <el-button type="primary"
                   @click="handleSave('form')">保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import Page from '@/base/page/page'
  import { mapGetters } from 'vuex'
  import VueNeditorWrap from 'vue-neditor-wrap'
  export default {
    name: "operationGuideList",
    data () {
      return {
        myConfig: {
          // 你的UEditor资源存放的路径,相对于打包后的index.html
          UEDITOR_HOME_URL: this.api.uEditorHomeUrl,
          // 编辑器不自动被内容撑高
          autoHeightEnabled: false,
          // 初始容器高度
          initialFrameHeight: '418',
          // 初始容器宽度
          initialFrameWidth: '100%',
          // 关闭自动保存
          enableAutoSave: false,
          scaleEnabled: true
        },
        tableData: [],
        dialogVisible: false,
        param: {
          title: '',
          content: '',
          type: '',
          sortNum: ''
        },
        rules: {
          content: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
          title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
          type: [{ required: true, message: '客户端类型不能为空', trigger: 'blur' }],
          sortNum: [{ required: true, type: 'number', message: '排序号必须为数字值', trigger: 'blur' }]
        },
        pageTotal: 0,
        options: [
          {
            value: 1,
            label: '个人端'
          },
          {
            value: 2,
            label: '医生端'
          }
        ]
      }
    },
    watch: {
      dialogVisible: function () {
        if (!this.dialogVisible) {
          this.$nextTick(() => {
            this.param = {
              title: '',
              content: '',
              type: '',
              sortNum: ''
            }
          })
        }
      }
    },
    mounted () {
      this.getList();
    },
    methods: {
      handleQuery () {
        this.page.pageIndex=0
        this.getList()
      },
      handleAdd () {
        this.dialogVisible = true
      },
      handleSave (form) {
        this.$refs[form].validate((valid) => {
          if (valid) {
            this.post(this.api.operationGuide.save, this.param).then(res => {
              if (res.data.statusCode === '200') {
                this.$message({
                  showClose: true,
                  message: "保存成功",
                  type: 'success'
                });
                this.dialogVisible = false;
                this.param = {
                  title: '',
                  content: '',
                  type: '',
                  sortNum: ''
                };
                this.getList()
              }
            })
          }
        })
      },
      getList () {
        let data = Object.assign({}, this.page, this.param);
        this.post(this.api.operationGuide.selectPageAll, data).then((res) => {
          if (res.data.code === '200') {
            this.tableData = res.data.object;
            this.pageTotal = res.data.totalNum
          }
        })
      },
      handleDel (row) {
        this.$Modal.confirm({
          title: '确认',
          content: '<p>您确认删除该数据？</p>',
          onOk: () => {
            let param = {
              id: row.id
            };
            this.post(this.api.operationGuide.delete, param).then(res => {
              if (res.data.code === '200') {
                this.$message({
                  showClose: true,
                  message: "删除成功",
                  type: 'success'
                });
              }
              this.getList()
            })
          },
          error: (data) => {
            this.$Message.error(data.message);
          },
          onCancel: () => { }
        });
      },
      closeForm () {
        this.dialogVisible = false;
        // this.getList()
      },
      handleEdit (row) {
        this.dialogVisible = true;
        this.param = row
      },
      formatterType (row) {
        if (row.type === 1) {
          return "个人端"
        }
        if (row.type === 2) {
          return "医生端"
        }
      },
      formatterDate (row) {
        return this.commonFunction.timestampFormat(new Date(row.createTime), 'yyyy-MM-dd hh:mm:ss')
      }
    },
    computed: {
      ...mapGetters([
        'page'
      ])
    },
    components: {
      Page,
      VueNeditorWrap
    }
  }
</script>

<style scoped>
  .editorForm {
    height: 750px;
  }
  .editor {
    position: fixed;
    margin-left: -70px;
    width: 70%;
    height: 700px;
    float: left;
  }
  .info {
    left: 68%;
    height: 750px;
    width: 30%;
    position: absolute;
    top: 78px;
  }
  .info .el-input {
    width: 500px;
  }
  .info .el-select {
    width: 500px;
  }
  .formTitle {
    display: inline-block;
    line-height: 24px;
    font-size: 18px;
    color: #303133;
  }

  .return {
    float: right;
  }
</style>
